/*
 * Copyright 2009 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package bw.note;

import java.util.*;
import bw.os.*;
import bw.util.*;

public class NotebookManager
{
    public static final String NOTEBOOK_INDEX = "WD-NOTEBOOK-INDEX";
    public static final String NOTEBOOK_INDEX_DEFAULT = "WD-NOTEBOOK-INDEX-DEFAULT";
    public static final String DEFAULT_PAGE_NAME = "NoteBookMain";

    public NotebookManager ()
    {
        //nothing
    }

    private Vector getPageNames ()
    {
        Vector pageNames = null;
        try
        {
            pageNames = (Vector) ObjectStore.getInstance().get( NOTEBOOK_INDEX );
        }
        catch ( Exception ex )
        {
            Log.getInstance().write( "Problem getting notebook index!", ex );
        }

        if ( pageNames == null ) { pageNames = new Vector(); }

        return pageNames;
    }

    public Vector getPages ()
    {        
        Vector pages = new Vector();

        Vector pageNames = getPageNames();

        for ( Enumeration e = pageNames.elements(); e.hasMoreElements(); )
        {
            String name = (String) e.nextElement();
            pages.addElement( new NotebookPage( name ) );
        }

        return pages;
    }

    public void addPage ( String name )
    {
        Vector pages = getPageNames();
        if ( !pages.contains( name ) )
        {
            pages.addElement( name );
        }

        storePages( pages );
    }

    public void deletePage ( String name )
    {
        Vector pages = getPageNames();
        pages.removeElement( name );

        //delete all the notes on this page too
        NoteManager manager = new NoteManager( name );
        manager.destroy();

        //if this is the default page, then set the default to the constant
        if ( name.equals( getDefaultPage() ) )
        {
            setDefaultPage( DEFAULT_PAGE_NAME );
        }

        storePages( pages );
    }

    public String getDefaultPage ()
    {
        String def = null;

        try
        {
            def = (String) ObjectStore.getInstance().get( NOTEBOOK_INDEX_DEFAULT );

            if ( def == null )
            {
                def = DEFAULT_PAGE_NAME;
            }
        }
        catch ( Exception ex )
        {
            Log.getInstance().write( "Problem getting notebook index default!", ex );
        }

        return def;
    }

    public void setDefaultPage ( String name )
    {
        try
        {
            ObjectStore.getInstance().put( NOTEBOOK_INDEX_DEFAULT, name );
        }
        catch ( Exception ex )
        {
            Log.getInstance().write( "Problem setting default notebook page: " + name, ex );
        }
    }

    private void storePages ( Vector pages )
    {
        try
        {
            ObjectStore.getInstance().put( NOTEBOOK_INDEX, pages );
        }
        catch ( Exception ex )
        {
            Log.getInstance().write( "Problem writing notebook index!", ex );
        }
    }
}